<template>
	<view style="display: flex;">
		<slot>
			<u-button type="primary" size="mini" @click="btntap">
				{{$z.$t(str)}}
			</u-button>
		</slot>
		<u-code ref="uCode" @change="codeChange"></u-code>
		<!-- <u-verification-code unique-key="page-b" ref="uCode" @change="codeChange"></u-verification-code> -->
	</view>
</template>

<script>
	export default {
		name: "wpasssmsbtn",

		data() {
			return {
				userutel: "",
				str: "",
				WpassSms: "",
				requtel: ""
			};
		},
		props: {
			utel: {
				type: String,
				default: ""
			}
		},
		methods: {
			async codeyz(yzm) {
				if (String(yzm).length != 6) {
					this.$u.toast("验证码错误")
					return Promise.reject();
				}
				let WpassSmsChkpar = await this.$z.request("/WpassSmsChk", {
					utel: this.requtel,
					sms: yzm,
				})
				if (WpassSmsChkpar[0].par != 'succeed') {
					this.$u.toast("验证码错误")
					return Promise.reject();
				} else {
					return
				}
			},
			codeChange(text) {
				this.str = text;
			},
			btntap() {
				if (this.$refs.uCode.canGetCode) {
					this.$emit("click");
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			async getCode(tel) {
				if (this.$refs.uCode.canGetCode) {
					let utel = this.utel == "" ? this.userutel : this.utel;
					if (tel) {
						utel = tel;
					}
					if (utel == "") {
						let data = await this.$store.dispatch("user/getUSearch")
						data = data[0];
						utel = data.utel || ""
						if (utel) {
							uni.$u.toast("获取验证码失败")
							return;
						}
					}
					this.requtel = utel
					let WpassSms = await this.$z.request("/WpassSms", {
						utel
					})
					WpassSms = WpassSms[0].par;
					if (WpassSms == "Notel") {
						this.$u.toast('验证码获取失败');
					} else {
						this.WpassSms = WpassSms;
						this.$u.toast('验证码已发送');
					}
					this.$refs.uCode.start();
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="scss">

</style>